/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#pragma once

#include <poll.h>

#include <unistd.h>
#include <fcntl.h>

#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>

namespace Hdfs {
namespace Mock {

class MockSockSysCallInterface {
public:
    virtual ~MockSockSysCallInterface() {
    }

    virtual ssize_t recv(int sock, void * buffer, size_t size, int flag) = 0;
    virtual ssize_t send(int sock, const void * buffer, size_t size,
            int flag) = 0;
  virtual ssize_t recvmsg(int socket, struct msghdr *message, int flags) = 0;
    virtual int getaddrinfo(const char * __restrict host,
            const char * __restrict port,
            const struct addrinfo * __restrict hint,
            struct addrinfo ** __restrict addr) = 0;
    virtual void freeaddrinfo(struct addrinfo * addr) = 0;
    virtual int socket(int family, int type, int protocol) = 0;
    virtual int connect(int sock, const struct sockaddr * addr,
            socklen_t len) = 0;
    virtual int getpeername(int sock, struct sockaddr * __restrict peer,
            socklen_t * __restrict len) = 0;
    virtual int fcntl(int sock, int flag, int value) = 0;
    virtual int setsockopt(int sock, int level, int optname, const void *optval,
            socklen_t optlen) = 0;
    virtual int poll(struct pollfd * pfd, nfds_t size, int timeout) = 0;
    virtual int shutdown(int sock, int how) = 0;
    virtual int close(int sock) = 0;
};

}
}

namespace MockSystem {

extern Hdfs::Mock::MockSockSysCallInterface * MockSockSysCallObj;

ssize_t recv(int sock, void * buffer, size_t size, int flag);

ssize_t send(int sock, const void * buffer, size_t size, int flag);

ssize_t recvmsg(int socket, struct msghdr *message, int flags);

int getaddrinfo(const char * __restrict host, const char * __restrict port,
        const struct addrinfo * __restrict hint,
        struct addrinfo ** __restrict addr);

void freeaddrinfo(struct addrinfo * addr);

int socket(int family, int type, int protocol);

int connect(int sock, const struct sockaddr * addr, socklen_t len);

int getpeername(int sock, struct sockaddr * __restrict peer,
        socklen_t * __restrict len);

int fcntl(int sock, int flag, int value);

int setsockopt(int sock, int level, int optname, const void *optval,
        socklen_t optlen);

int poll(struct pollfd * pfd, nfds_t size, int timeout);

int shutdown(int sock, int how);

int close(int sock);

}
